#pragma once

#include "FrontEndController.h"
#include <iostream>
#include <cstdio>
#include <ctime>
#include <iterator>
#include <sstream>

using namespace std;


int main(int argc, string argv[])
{
	cout << "SimpleSix - CLI interface for exploratory testing.\n\n";
	FrontEndController *controller = new FrontEndController();
	
	string result = "";
	string sourcename = "";
	string query = "";
	vector<string> results;

	// If name of source file is included as argument.
	if (argc == 2) { 
		sourcename = argv[1];
		cout << "Source file entered: " << sourcename << "\n";
		cout << "Parsing source file now...\n";
		controller->frontEndParse(sourcename);
	}
	// if user wants to manually input source code, no arguments needed.
	else {
		cout << "Enter file name of source code to test:\n";
		getline(cin, sourcename);
		cout << "Parsing source file now...\n";
		controller->frontEndParse(sourcename);
	}

	cin.clear();
	cout << "Simple sourcecode successfully parsed.\nType <exit> at any time to exit.\n\n";
	
	while(1) {
		cout << "Please enter your query:\n";
		getline(cin, query);

		if ( query == "exit") {
			exit(0);
		}

		// Time how long evaluation took
		std::clock_t start;
		double duration;

		start = std::clock();

		cout << "Query entered: \"" << query << "\"" << endl;
		results = controller->frontEndEvaluate(query);

		duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
		
		std::cout << "Time for evaluation: " << duration << " seconds\n";

		std::sort(results.begin(), results.end());
		
		cout << "Results: " ;
		for(unsigned int i = 0; i < results.size(); i++) {
			std::string result = results.at(i);
			cout << result;
			if ( i != (results.size()-1) ) {
				cout << ", "; // comma except for the last element
			}
		}
		cout << "\n";
	}

	return 0;
}